<?php
/*
Plugin Name: LeadingPerPost 
Plugin URI: http://www.xys-reader.org/blogs
Description: Allow blog user to show only the leading part of blog on home page.
Version: 0.1 
Author: Liu Xuefeng
Author URI: http://www.xys-reader.org/blogs/sys_admin
*/
function complete_mark($content,$mark){
                $num1=0;$num2=0;
                preg_match_all("/(<${mark}[^\/>]*>)/i",$content,$matched1,PREG_PATTERN_ORDER);
                preg_match_all("/(<\/${mark})/i",$content,$matched2,PREG_PATTERN_ORDER);
                $num1=count($matched1[1]);
                $num2=count($matched2[1]);
                for($i=0;$i<$num1-$num2;$i++) $content.="</${mark}>\n";
                for($i=0;$i<$num2-$num1;$i++) $content ="<${mark}>\n".$content;
		return $content;
/*
*/
}
/*
add_action('wp_head', 'my_wp_head');
function my_wp_head() {
        echo "<META NAME='KEYWORDS' CONTENT='"; //PEAK2008
        bloginfo('name');
        echo ", XYS-READER'/>\n";
        if( is_home()){
                echo "<meta NAME='description' content=\" Welcome to ";
                bloginfo('name');
                echo " at  德赛读者网 http://www.XYS-READER.org .\n";
                //echo " Recent Post:\n <ul>\n ";
                //wp_get_archives('type=postbypost&limit=8');
                //echo " </ul>";
                echo "\"/>";
        }

}
*/
add_filter('the_content', 'concise_content');
function concise_content($content,$word_count=0,$is_portal=0) {

	if( $word_count>0 ||( get_option("part_per_post") && is_home()) ){

		if(preg_match('/Continue Reading/',$content) ){
			return $content;
		}

		if($word_count<10) $word_count=252;

                $content = preg_replace('/\[if(.*)\[endif\]/i',"", $content); #PEAK ADD|To deal with sina blog
	
                $content = preg_replace('/<!--([^>]*)-->/i',"", $content); #PEAK ADD|To deal with sina blog
                $content = preg_replace('/<h([^>]*)>/i',"", $content); #PEAK ADD|To deal with sina blog
                $content = preg_replace('/<\/h([^>]*)>/i',"", $content); #PEAK ADD|To deal with sina blog
                $content = preg_replace('/<p([^>]*)>/i'," <p>", $content); #PEAK ADD|To deal with sina blog

                $content = preg_replace('/<div([^>]*)>/i'," <div>", $content); #PEAK ADD|To deal with sina blog
                $content = preg_replace('/<span([^>]*)>/i'," <span>", $content); #PEAK ADD|To deal with sina blog

                $content = preg_replace('/<img([^>]*)>/i',"", $content); #PEAK ADD|To deal with sina blog
                $content = preg_replace('/\[img\]([^\[]+)\[\/img\]/i',"", $content); #PEAK ADD|To deal with sina blog
                $content = preg_replace('/<a([^>]*)>/i'," ", $content); #PEAK ADD|To deal with sina blog
                $content = preg_replace('/<\/?span([^>]*)>/i',"", $content); #PEAK ADD|To deal with sina blog
                $content = preg_replace('/<\/?strong([^>]*)>/i',"", $content); #PEAK ADD|To deal with sina blog
                //$content = preg_replace('/<p[^re]([^>]*)>/i',"<p>", $content); #PEAK ADD|To deal with sina blog
                $content = preg_replace('/<\/?font([^>]*)>/i',"", $content); #PEAK ADD|To deal with sina blog

                $content = preg_replace('/<\/?td([^>]*)>/i',"", $content); #PEAK ADD|To deal with tables
                $content = preg_replace('/<tr([^>]*)>/i',"", $content); #PEAK ADD|To deal with tables
                $content = preg_replace('/<\/tr([^>]*)>/i',"<br>", $content); #PEAK ADD|To deal with tables
                $content = preg_replace('/<table([^>]*)>/i',"", $content); #PEAK ADD|To deal with tables
                $content = preg_replace('/<tbody([^>]*)>/i',"", $content); #PEAK ADD|To deal with tables
 
                mb_internal_encoding("UTF-8");
                $content = preg_replace('/<([^>]*)$/i',"", $content); #PEAK ADD|To deal with sina blog

                $content = mb_substr($content, 0,$word_count);


                $content = preg_replace('/<([^>]{1,9})$/i'," ", $content); #PEAK ADD|To deal with sina blog


		$content = complete_mark($content, "div");
		$content = complete_mark($content, "font");
		$content = complete_mark($content, "b");
		$content = complete_mark($content, "blockquote");
		$content = complete_mark($content, "em");


                $num1=0;$num2=0;
                preg_match_all("/(<p[^re])/",$content,$matched1,PREG_PATTERN_ORDER);
                preg_match_all("/(<\/p[^re])/",$content,$matched2,PREG_PATTERN_ORDER);
                $num1=count($matched1[1]);
                $num2=count($matched2[1]);
                for($i=0;$i<$num1-$num2;$i++) $content.="</p>\n";
		
		$content = complete_mark($content, "pre");


		$content = complete_mark($content, "table");
		


                $content.="...\n";
		if(!$is_portal){
		$content.="<br><div align=right><a href=".get_permalink();
		$content .= "> ". __("More ...") ."</a> &nbsp; </div>";
		}


		return $content;
        }
	else{

                $num1=0;$num2=0;
                preg_match_all("/(<div[\/>]*>)/",$content,$matched1,PREG_PATTERN_ORDER);
                preg_match_all("/(<\/div)/",$content,$matched2,PREG_PATTERN_ORDER);
                $num1=count($matched1[1]);
                $num2=count($matched2[1]);
                for($i=0;$i<$num1-$num2;$i++) $content.="</div>\n";

                $num1=0;$num2=0;
                preg_match_all("/(<blockquote[\/>]*>)/",$content,$matched1,PREG_PATTERN_ORDER);
                preg_match_all("/(<\/blockquote)/",$content,$matched2,PREG_PATTERN_ORDER);
                $num1=count($matched1[1]);
                $num2=count($matched2[1]);
                for($i=0;$i<$num1-$num2;$i++) $content.="</blockquote>\n";

	}
	return $content;
}
?>
